﻿using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.ViewModels;
using System.Collections.Generic;
using Fayshine.VA.RIAService.Web;
using System.Windows.Controls.Theming;
using System.Windows;
using Fayshine.VA.Helpers;

namespace Fayshine.VA.Views.SysConfig
{
    public partial class DeviceManage : Page
    {

        private DeviceCameraViewModel m_ViewModel = null;

        public DeviceManage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DeviceManage_Loaded);
        }

        void DeviceManage_Loaded(object sender, RoutedEventArgs e)
        {
            ValidUserRole();
            m_ViewModel = new DeviceCameraViewModel();
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllDevicess();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnAddDevice_Click(object sender, RoutedEventArgs e)
        {
            DeviceWindow addWindow = new DeviceWindow();
            addWindow.Closed += new System.EventHandler(addWindow_Closed);
            addWindow.Show();
            
        }

        void addWindow_Closed(object sender, System.EventArgs e)
        {
            DeviceWindow deviceWindow = sender as DeviceWindow;
            if (deviceWindow.SelectedDevice != null && deviceWindow.DialogResult.HasValue && deviceWindow.DialogResult.Value)
            {
                if (!m_ViewModel.ExistDevice(deviceWindow.SelectedDevice.HostName))
                {
                    m_ViewModel.AddDevice(deviceWindow.SelectedDevice);
                }
                else
                {
                    MessageBox.Show("设备 " + deviceWindow.SelectedDevice.HostName + " 已经存在！");
                }

            }
        }

        private void btnDeleteDevuce_Click(object sender, RoutedEventArgs e)
        {
            var selectedDevices = this.dataGrid.GetSelectedItems<vaserver>();
            if (selectedDevices.Count <= 0)
            {
                MessageBox.Show("请选择要删除的设备");
            }
            else
            {
                if (MessageBox.Show("确定要删除选择的设备吗?", "确定", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    if (m_ViewModel != null)
                    {
                        m_ViewModel.DeleteDevices(selectedDevices);
                    }
                }
            }
        }

        private void btnUpdateDevice_Click(object sender, RoutedEventArgs e)
        {
            vaserver selectedUser = this.dataGrid.SelectedItem as vaserver;
            if (selectedUser == null)
            {
                MessageBox.Show("请选择要编辑的设备");
            }
            else
            {
                DeviceWindow editUserWindow = new DeviceWindow(selectedUser);
                editUserWindow.Closed += new System.EventHandler(editUserWindow_Closed);
                editUserWindow.Show();
            }
        }

        void editUserWindow_Closed(object sender, System.EventArgs e)
        {
            DeviceWindow userWindow = sender as DeviceWindow;
            if (userWindow.SelectedDevice != null && userWindow.DialogResult.HasValue && userWindow.DialogResult.Value)
            {
                m_ViewModel.Update(userWindow.SelectedDevice, userWindow.DeleteCameras);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.m_ViewModel != null)
            {
                this.m_ViewModel.SaveData2();
            }
        }

        private void dataGrid_RowDoubleClicked(object sender, Control.DataGridRowClickedArgs e)
        {
            vaserver selectedUser = this.dataGrid.SelectedItem as vaserver;
            if (selectedUser == null)
            {
                MessageBox.Show("请选择要编辑的设备");
            }
            else
            {
                DeviceWindow editUserWindow = new DeviceWindow(selectedUser);
                editUserWindow.Closed += new System.EventHandler(editUserWindow_Closed);
                editUserWindow.Show();
            }
        }

        private void ValidUserRole()
        {
            user user = SessionManager.Session["UserInfo"] as user;
            if (user != null)
            {
                if (user.RoleID == 3)
                {
                    this.btnAddDevice.IsEnabled = false;
                    this.btnDeleteDevuce.IsEnabled = false;
                    this.btnSave.IsEnabled = false;
                    this.btnUpdateDevice.IsEnabled = false;
                    this.dataGrid.IsReadOnly = true;
                }
                else
                {
                    this.btnAddDevice.IsEnabled = true;
                    this.btnDeleteDevuce.IsEnabled = true;
                    this.btnSave.IsEnabled = true;
                    this.btnUpdateDevice.IsEnabled = true;
                    this.dataGrid.IsReadOnly = false;
                }
            }
        }

    }
}
